namespace QMVnSoft.DAL.Util
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Cryptography;

    public static class AppSecurity
    {
        public static bool SoftwareDecode(int pi_CodeNumber, string ps_Chars1, string ps_Chars2, string ps_Chars3, string ps_Chars4, string ps_Chars5)
        {
            if (((((ps_Chars1.Length != 4) || (ps_Chars2.Length != 4)) || ((ps_Chars3.Length != 4) || (ps_Chars4.Length != 4))) || (ps_Chars5.Length != 4)) || ((((ps_Chars1 == ps_Chars2) && (ps_Chars2 == ps_Chars3)) && ((ps_Chars3 == ps_Chars4) && (ps_Chars4 == ps_Chars5))) && (ps_Chars5 == "0000")))
            {
                return false;
            }
            char[] chArray1 = ps_Chars1.ToCharArray();
            char[] chArray2 = ps_Chars2.ToCharArray();
            char[] chArray3 = ps_Chars3.ToCharArray();
            char[] chArray4 = ps_Chars4.ToCharArray();
            char[] chArray5 = ps_Chars5.ToCharArray();
            int num = 0;
            for (int i = 0; i < 4; i++)
            {
                num = ((chArray1[i] + chArray4[i]) + chArray5[i]) - pi_CodeNumber;
                if (num < 0x30)
                {
                    num = 0x30;
                }
                else if (num > 90)
                {
                    num = 90;
                }
                else if ((num > 0x39) && (num < 0x41))
                {
                    if (num < 0x3d)
                    {
                        num = 0x39;
                    }
                    else
                    {
                        num = 0x41;
                    }
                }
                if (chArray3[i] != ((char)num))
                {
                    return false;
                }
            }
            for (int j = 0; j < 4; j++)
            {
                num = (((chArray1[j] + chArray3[j]) + chArray4[j]) + chArray5[j]) - 0xef;
                if (num < 0x30)
                {
                    num = 0x30;
                }
                else if (num > 90)
                {
                    num = 90;
                }
                else if ((num > 0x39) && (num < 0x41))
                {
                    if (num < 0x3d)
                    {
                        num = 0x39;
                    }
                    else
                    {
                        num = 0x41;
                    }
                }
                if (chArray2[j] != ((char)num))
                {
                    return false;
                }
            }
            return true;
        }

        public static string SoftwareEncode(int pi_CodeNumber)
        {
            char[] chArray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            char[] chArray2 = new char[4];
            char[] chArray3 = new char[4];
            char[] chArray4 = new char[4];
            char[] chArray5 = new char[4];
            char[] chArray6 = new char[4];
            int num = 0;
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                chArray2[i] = chArray[random.Next(0x24)];
            }
            for (int j = 0; j < 4; j++)
            {
                chArray5[j] = chArray[random.Next(0x24)];
            }
            for (int k = 0; k < 4; k++)
            {
                chArray6[k] = chArray[random.Next(0x24)];
            }
            for (int m = 0; m < 4; m++)
            {
                num = ((chArray2[m] + chArray5[m]) + chArray6[m]) - pi_CodeNumber;
                if (num < 0x30)
                {
                    num = 0x30;
                }
                else if (num > 90)
                {
                    num = 90;
                }
                else if ((num > 0x39) && (num < 0x41))
                {
                    if (num < 0x3d)
                    {
                        num = 0x39;
                    }
                    else
                    {
                        num = 0x41;
                    }
                }
                chArray4[m] = (char)num;
            }
            for (int n = 0; n < 4; n++)
            {
                num = (((chArray2[n] + chArray4[n]) + chArray5[n]) + chArray6[n]) - 0xef;
                if (num < 0x30)
                {
                    num = 0x30;
                }
                else if (num > 90)
                {
                    num = 90;
                }
                else if ((num > 0x39) && (num < 0x41))
                {
                    if (num < 0x3d)
                    {
                        num = 0x39;
                    }
                    else
                    {
                        num = 0x41;
                    }
                }
                chArray3[n] = (char)num;
            }
            return (new string(chArray2) + "-" + new string(chArray3) + "-" + new string(chArray4) + "-" + new string(chArray5) + "-" + new string(chArray6));
        }

        public static string Base64Decode(string ps_String)
        {
            string str2;
            try
            {
                System.Text.Decoder decoder = new UTF8Encoding().GetDecoder();
                byte[] bytes = Convert.FromBase64String(ps_String);
                char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)];
                decoder.GetChars(bytes, 0, bytes.Length, chars, 0);
                string str = new string(chars);
                str2 = str;
            }
            catch (Exception exception)
            {
                throw new Exception("Error in base64Decode" + exception.Message);
            }
            return str2;
        }

        public static string Base64Encode(string ps_String)
        {
            string str2;
            try
            {
                byte[] buffer = new byte[ps_String.Length];
                str2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(ps_String));
            }
            catch (Exception exception)
            {
                throw new Exception("Error in base64Encode" + exception.Message);
            }
            return str2;
        }

        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}
